from ansible import constants
from ansible.executor.task_queue_manager import TaskQueueManager
from ansible.inventory.manager import InventoryManager
from ansible.parsing.dataloader import DataLoader
from ansible.playbook.play import Play
from ansible.plugins.callback import CallbackBase
from ansible.vars.manager import VariableManager

loader = DataLoader()
inventory = InventoryManager(loader=loader, sources='inventory.ini')
variable_manager = VariableManager(loader=loader, inventory=inventory)
play_source = dict(
    name="Run ls command using ad-hoc module",
    hosts='all',
    gather_facts='no',
    tasks=[
        dict(action=dict(module='shell', args='ls')),
    ]
)
play = Play().load(play_source, variable_manager=variable_manager, loader=loader)
tqm = TaskQueueManager(
    inventory=inventory,
    variable_manager=variable_manager,
    loader=loader,
    passwords=dict(),
    # options=dict(forks=4, verbosity=5),
    # stdout_callback=CallbackBase(),
)
result = tqm.run(play)
